package cn.edu.qfnu.oj.service.impl;

import cn.edu.qfnu.common.exception.db.NoRecordFoundException;
import cn.edu.qfnu.common.exception.db.RecordAlreadyExistedException;
import cn.edu.qfnu.common.exception.db.WrongParameterException;
import cn.edu.qfnu.oj.dao.LevelDao;
import cn.edu.qfnu.oj.dao.StudentDao;
import cn.edu.qfnu.oj.dao.TeacherDao;
import cn.edu.qfnu.oj.entity.Admin;
import cn.edu.qfnu.oj.entity.Level;
import cn.edu.qfnu.oj.entity.Student;
import cn.edu.qfnu.oj.entity.Teacher;
import cn.edu.qfnu.oj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("userService")
public class UserServiceImpl implements UserService {

    private final Admin admin = Admin.getAdminUser();

    private final StudentDao studentDao;
    private final TeacherDao teacherDao;
    private final LevelDao levelDao;

    @Autowired
    public UserServiceImpl(StudentDao studentDao, TeacherDao teacherDao, LevelDao levelDao) {
        this.studentDao = studentDao;
        this.teacherDao = teacherDao;
        this.levelDao = levelDao;
    }

    /**
     * 学生注册
     * @param student 要注册的学生
     */
    @Override
    public void register(Student student)
            throws RecordAlreadyExistedException {
        if (studentDao.isRepeat(student)) {
            throw new RecordAlreadyExistedException("学生已存在");
        }
        Level level = new Level(student);
        studentDao.save(student);
        levelDao.save(level);
    }

    @Override
    public Object login(String username, String password, String identity)
            throws NoRecordFoundException, WrongParameterException {
        switch (identity) {
            case "学生":
                Student student = studentDao.loginCheck(username);
                if (student.getPassword().equals(password)) {
                    return student;
                } else {
                    throw new WrongParameterException("密码错误");
                }
            case "教师":
                Teacher teacher = teacherDao.loginCheck(username);
                if (teacher.getPassword().equals(password)) {
                    return teacher;
                } else {
                    throw new WrongParameterException("密码错误");
                }
            case "管理员":
                if (username.equals(admin.getUsername()) && password.equals(admin.getPassword())) {
                    return admin;
                } else {
                    throw new WrongParameterException("管理员身份验证失败");
                }
            default:
                throw new WrongParameterException("参数不正确");
        }
    }

    @Override
    public boolean isExist(String id, String email) throws WrongParameterException {
        return studentDao.isExist(id, email) || teacherDao.isExist(id, email);
    }

    public Object isWho(String id, String email) {
        if (studentDao.isExist(id, email)) {
            return studentDao.get(id);
        } else if (teacherDao.isExist(id, email)) {
            return teacherDao.get(id);
        } else {
            return null;
        }
    }

    public void update(Object user) {
        if (user instanceof Student) {
            studentDao.saveOrUpdate((Student) user);
        } else if (user instanceof Teacher) {
            teacherDao.saveOrUpdate((Teacher) user);
        }
    }

    public Object pwdUpdate(Object user, String old, String newPwd) throws WrongParameterException {
        if (user instanceof Student) {
            if (((Student) user).getPassword().equals(old)) {
                ((Student) user).setPassword(newPwd);
                return user;
            } else throw new WrongParameterException("旧密码错误");
        } else if (user instanceof Teacher) {
            if (((Teacher) user).getPassword().equals(old)) {
                ((Teacher) user).setPassword(newPwd);
                return user;
            } else throw new WrongParameterException("旧密码错误");
        } else {
            if (((Admin) user).getPassword().equals(old)) {
                ((Admin) user).setPassword(newPwd);
                return user;
            } else throw new WrongParameterException("旧密码错误");
        }
    }
}
